package com.bootcamp.javase2.threadpool;

import java.util.Random;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @program: day21CallableCode
 * @description:
 * @author: Keyu Li
 * @create: 2021-09-15 15:06
 **/

public class ThreadPoolSleep {
    public static void main(String[] args) {
        ExecutorService pool = Executors.newFixedThreadPool(2);
//        RandomSleep sleep = new RandomSleep();
        for (int i = 0; i < 10; i++) {
//            pool.submit(sleep);
            pool.submit(new RandomSleep());
        }
        pool.shutdown();
    }
}

class RandomSleep implements Runnable {
    Random random = new Random();

    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName()
                + " sleep begin!!!");
        int time = random.nextInt(10) + 1;
        try {
            Thread.sleep(time);
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            System.out.println(Thread.currentThread().getName() + " sleeping....");
        }
        System.out.println(Thread.currentThread().getName()
                + " has already slept " + time
                + " seconds, time to get up!!!");
    }
}